package com.rc.activity.sync.util;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;

import com.rc.activity.dao.DaoSession;
import com.rc.activity.dao.tbl_Receipt;
import com.rc.activity.dao.tbl_ReceiptDao;
import com.rc.net.NetSupport;

public class ReceiptSync {

	public static String[] keys = { "status", "ReceiptID", "messages" };

	private Context context;
	private DaoSession daoSession;
	public String query;
	private tbl_ReceiptDao daoReceipt;
	private SQLiteDatabase db;
	public Cursor cursor;

	public ReceiptSync(Context context, SQLiteDatabase db, DaoSession daoSession) {
		this.context = context;
		this.db = db;
		this.daoSession = daoSession;
		daoReceipt = this.daoSession.getTbl_ReceiptDao();
	}

	/**
	 * Delete multi receipt
	 */
	public void deleteReceipt() {
		new callDeleteReceipt().execute("");
	}

	/**
	 * Insert receipt
	 */
	public void insertRecipt() {
		new callInsertReceipt().execute("");
	}

	/**
	 * Update receipt
	 */
	public void updateRecipt() {
		new callUpdateReceipt().execute("");
	}

	/**
	 * call multidelteReceipt
	 */
	private class callDeleteReceipt extends
			AsyncTask<String, Void, ArrayList<Long>> {

		public callDeleteReceipt() {
			super();
		}

		@Override
		protected ArrayList<Long> doInBackground(String... params) {
			// The string name is query, providing that "isDelete" = 1
			query = tbl_ReceiptDao.Properties.IsDelete.columnName + " = '1'";
			ArrayList<tbl_Receipt> arrReceipt;
			ArrayList<Long> arrReceiptID = new ArrayList<Long>();

			// get data in sqlite
			cursor = db.query(daoReceipt.getTablename(),
					daoReceipt.getAllColumns(), query, null, null, null, null);
			arrReceipt = new ArrayList<tbl_Receipt>();

			// get ArrayList Receipt by cursor
			arrReceipt = tbl_Receipt.getObjectbyCursor(cursor);
			if (arrReceipt.size() > 0) {
				for (tbl_Receipt objReceipt : arrReceipt) {
					// get ID receipt
					arrReceiptID.add(objReceipt.getReceiptID());
				}
				// query server
				if (NetSupport.delete_receipt(arrReceiptID, context)) {
					return arrReceiptID;
				}
			}
			return null;

		}

		protected void onPostExecute(ArrayList<Long> arrReceiptID) {
			insertRecipt();
		}
	}

	/**
	 * call multiInsertReceipt
	 */
	private class callInsertReceipt extends
			AsyncTask<String, Void, ArrayList<tbl_Receipt>> {

		@Override
		protected ArrayList<tbl_Receipt> doInBackground(String... params) {
			// The string name is query, providing that "isDelete" = 1
			query = tbl_ReceiptDao.Properties.IsCreate.columnName + " = '1'";
			ArrayList<tbl_Receipt> arrReceipt;

			// get data in sqlite
			cursor = db.query(daoReceipt.getTablename(),
					daoReceipt.getAllColumns(), query, null, null, null, null);
			arrReceipt = new ArrayList<tbl_Receipt>();

			// get ArrayList Receipt by cursor
			arrReceipt = tbl_Receipt.getObjectbyCursor(cursor);
			if (arrReceipt.size() > 0) {

				// query server
				if (NetSupport.insert_receipt(arrReceipt, context)) {
					return arrReceipt;
				}
			}
			return null;

		}

		protected void onPostExecute(ArrayList<tbl_Receipt> arrReceiptID) {
			updateRecipt();
		}
	}

	/**
	 * call multiInsertReceipt
	 */
	private class callUpdateReceipt extends
			AsyncTask<String, Void, ArrayList<tbl_Receipt>> {

		@Override
		protected ArrayList<tbl_Receipt> doInBackground(String... params) {
			// The string name is query, providing that "isDelete" = 1
			query = tbl_ReceiptDao.Properties.IsUpdate.columnName + " = '1'";
			ArrayList<tbl_Receipt> arrReceipt;

			// get data in sqlite
			cursor = db.query(daoReceipt.getTablename(),
					daoReceipt.getAllColumns(), query, null, null, null, null);
			arrReceipt = new ArrayList<tbl_Receipt>();

			// get ArrayList Receipt by cursor
			arrReceipt = tbl_Receipt.getObjectbyCursor(cursor);
			if (arrReceipt.size() > 0) {

				// query server
				if (NetSupport.update_receipt(arrReceipt, context)) {
					return arrReceipt;
				}
			}
			return null;

		}

		protected void onPostExecute(ArrayList<tbl_Receipt> arrReceiptID) {
		}
	}
}
